﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Weight.Base;

namespace Weight.Controls
{
    public class LYFForm : Form
    {
        private Size NormalSize = new Size(320, 240);

        private Panel TopBorderBar;
        private Panel BottomBorderBar;
        private Panel RightBorderBar;
        private Panel LeftBorderBar;

        private int borderBarWidth = 2;

        private Rectangle TopBorderRect;
        private Rectangle BottomBorderRect;
        private Rectangle RightBorderRect;
        private Rectangle LeftBorderRect;

        private MouseMoveStatus leftMouseMove = default(MouseMoveStatus);
        private MouseMoveStatus topMouseMove = default(MouseMoveStatus);
        private MouseMoveStatus rightMouseMove = default(MouseMoveStatus);
        private MouseMoveStatus bottomMouseMove = default(MouseMoveStatus);

        private bool leftMouseDown = false;
        private bool topMouseDown = false;
        private bool rightMouseDown = false;
        private bool bottomMouseDown = false;


        public LYFForm()
        {
            InitializeComponent();
            LeftBorderRect = new Rectangle(0, 0, borderBarWidth, LeftBorderBar.Height);
            TopBorderRect = new Rectangle(0, 0, TopBorderBar.Width, borderBarWidth);
            RightBorderRect = new Rectangle(RightBorderBar.Width - borderBarWidth, 0, borderBarWidth, RightBorderBar.Height);
            BottomBorderRect = new Rectangle(0, BottomBorderBar.Height - borderBarWidth, BottomBorderBar.Width, borderBarWidth);
           
        }

        private Color _borderColor = Color.FromArgb(0, 163, 129);
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(Color), "Color.FromArgb(0, 163, 129)")]
        public Color BorderColor
        {
            get => _borderColor;
            set
            {
                _borderColor = value;
                TopBorderBar.BackColor = _borderColor;
                BottomBorderBar.BackColor = _borderColor;
                RightBorderBar.BackColor = _borderColor;
                LeftBorderBar.BackColor = _borderColor;
            }
        }


        private bool _isBorder = true;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(bool), "true")]
        public bool IsBorder
        {
            get => _isBorder;
            set
            {
                _isBorder = value;
                LeftBorderBar.Visible = _isBorder;
                TopBorderBar.Visible = _isBorder;
                RightBorderBar.Visible = _isBorder;
                BottomBorderBar.Visible = _isBorder;
            }
        }

        //是否可以拖动窗体来改变大小
        private bool _isDragging = true;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(bool), "true")]
        public bool IsDragging
        {
            get => _isDragging;
            set
            {
                _isDragging = value;
            }
        }

        private void InitializeComponent()
        {
            this.LeftBorderBar = new System.Windows.Forms.Panel();
            this.TopBorderBar = new System.Windows.Forms.Panel();
            this.BottomBorderBar = new System.Windows.Forms.Panel();
            this.RightBorderBar = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // LeftBorderBar
            // 
            this.LeftBorderBar.BackColor = System.Drawing.Color.FromArgb(0, 163, 129);
            this.LeftBorderBar.Cursor = System.Windows.Forms.Cursors.Default;
            this.LeftBorderBar.Dock = System.Windows.Forms.DockStyle.Left;
            this.LeftBorderBar.Location = new System.Drawing.Point(0, 0);
            this.LeftBorderBar.Name = "LeftBorderBar";
            this.LeftBorderBar.Size = new System.Drawing.Size(2, 132);
            this.LeftBorderBar.TabIndex = 0;
            this.LeftBorderBar.SizeChanged += new System.EventHandler(this.BorderBar_SizeChanged);
            this.LeftBorderBar.MouseDown += new System.Windows.Forms.MouseEventHandler(this.BorderBar_MouseDown);
            this.LeftBorderBar.MouseMove += new System.Windows.Forms.MouseEventHandler(this.BorderBar_MouseMove);
            this.LeftBorderBar.MouseUp += new System.Windows.Forms.MouseEventHandler(this.LeftBorderBar_MouseUp);
            // 
            // TopBorderBar
            // 
            this.TopBorderBar.BackColor = System.Drawing.Color.FromArgb(0, 163, 129);
            this.TopBorderBar.Cursor = System.Windows.Forms.Cursors.Default;
            this.TopBorderBar.Dock = System.Windows.Forms.DockStyle.Top;
            this.TopBorderBar.Location = new System.Drawing.Point(2, 0);
            this.TopBorderBar.Name = "TopBorderBar";
            this.TopBorderBar.Size = new System.Drawing.Size(234, 3);
            this.TopBorderBar.TabIndex = 1;
            this.TopBorderBar.SizeChanged += new System.EventHandler(this.BorderBar_SizeChanged);
            this.TopBorderBar.MouseDown += new System.Windows.Forms.MouseEventHandler(this.BorderBar_MouseDown);
            this.TopBorderBar.MouseMove += new System.Windows.Forms.MouseEventHandler(this.BorderBar_MouseMove);
            this.TopBorderBar.MouseUp += new System.Windows.Forms.MouseEventHandler(this.LeftBorderBar_MouseUp);
            // 
            // BottomBorderBar
            // 
            this.BottomBorderBar.BackColor = System.Drawing.Color.FromArgb(0, 163, 129);
            this.BottomBorderBar.Cursor = System.Windows.Forms.Cursors.Default;
            this.BottomBorderBar.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.BottomBorderBar.Location = new System.Drawing.Point(2, 129);
            this.BottomBorderBar.Name = "BottomBorderBar";
            this.BottomBorderBar.Size = new System.Drawing.Size(234, 3);
            this.BottomBorderBar.TabIndex = 2;
            this.BottomBorderBar.SizeChanged += new System.EventHandler(this.BorderBar_SizeChanged);
            this.BottomBorderBar.MouseDown += new System.Windows.Forms.MouseEventHandler(this.BorderBar_MouseDown);
            this.BottomBorderBar.MouseMove += new System.Windows.Forms.MouseEventHandler(this.BorderBar_MouseMove);
            this.BottomBorderBar.MouseUp += new System.Windows.Forms.MouseEventHandler(this.LeftBorderBar_MouseUp);
            // 
            // RightBorderBar
            // 
            this.RightBorderBar.BackColor = System.Drawing.Color.FromArgb(0, 163, 129);
            this.RightBorderBar.Cursor = System.Windows.Forms.Cursors.Default;
            this.RightBorderBar.Dock = System.Windows.Forms.DockStyle.Right;
            this.RightBorderBar.Location = new System.Drawing.Point(236, 0);
            this.RightBorderBar.Name = "RightBorderBar";
            this.RightBorderBar.Size = new System.Drawing.Size(2, 132);
            this.RightBorderBar.TabIndex = 0;
            this.RightBorderBar.SizeChanged += new System.EventHandler(this.BorderBar_SizeChanged);
            this.RightBorderBar.MouseDown += new System.Windows.Forms.MouseEventHandler(this.BorderBar_MouseDown);
            this.RightBorderBar.MouseMove += new System.Windows.Forms.MouseEventHandler(this.BorderBar_MouseMove);
            this.RightBorderBar.MouseUp += new System.Windows.Forms.MouseEventHandler(this.LeftBorderBar_MouseUp);
            // 
            // CoolForm
            // 
            this.ClientSize = new System.Drawing.Size(238, 132);
            this.Controls.Add(this.BottomBorderBar);
            this.Controls.Add(this.TopBorderBar);
            this.Controls.Add(this.RightBorderBar);
            this.Controls.Add(this.LeftBorderBar);
            this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "CoolForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.MaximumSizeChanged += new System.EventHandler(this.CoolForm_MaximumSizeChanged);
            this.ResumeLayout(false);

        }

        private void CoolForm_MaximumSizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                Size size = BaseControls.Instance.GetPrimaryScreenWorkingArea();
                BaseControls.Instance.SetValueReflection(this, size, "MaximumSize");
            }
            BaseControls.Instance.SetValueReflection(this, this.WindowState, "WindowState");
        }

        private void BorderBar_MouseDown(object sender, MouseEventArgs e)
        {
            Panel bar = sender as Panel;
            Point pt = new Point(e.X, e.Y);
            if (bar.Name == "LeftBorderBar")
            {
                if (LeftBorderRect.Contains(pt))
                    leftMouseDown = true;
            }
            else if (bar.Name == "TopBorderBar")
            {
                if (TopBorderRect.Contains(pt))
                    topMouseDown = true;
            }
            else if (bar.Name == "RightBorderBar")
            {
                if (RightBorderRect.Contains(pt))
                    rightMouseDown = true;
            }
            else if (bar.Name == "BottomBorderBar")
            {
                if (BottomBorderRect.Contains(pt))
                    bottomMouseDown = true;
            }
        }

        private void LeftBorderBar_MouseUp(object sender, MouseEventArgs e)
        {
            Panel bar = sender as Panel;
            Point pt = new Point(e.X, e.Y);
            if (bar.Name == "LeftBorderBar")
            {
                //if (LeftBorderRect.Contains(pt))
                leftMouseDown = false;
            }
            else if (bar.Name == "TopBorderBar")
            {
                //if (TopBorderRect.Contains(pt))
                topMouseDown = false;
            }
            else if (bar.Name == "RightBorderBar")
            {
                //if (RightBorderRect.Contains(pt))
                rightMouseDown = false;
            }
            else if (bar.Name == "BottomBorderBar")
            {
                //if (BottomBorderRect.Contains(pt))
                bottomMouseDown = false;
            }
        }

        private void BorderBar_SizeChanged(object sender, EventArgs e)
        {
            Panel bar = sender as Panel;
            if (bar.Name == "LeftBorderBar")
            {
                if (bar.Width < borderBarWidth)
                    bar.Width = borderBarWidth;
                LeftBorderRect = new Rectangle(0, 0, borderBarWidth, LeftBorderBar.Height);
            }
            else if (bar.Name == "TopBorderBar")
            {
                if (bar.Height < borderBarWidth)
                    bar.Height = borderBarWidth;
                TopBorderRect = new Rectangle(0, 0, TopBorderBar.Width, borderBarWidth);
            }
            else if (bar.Name == "RightBorderBar")
            {
                if (bar.Width < borderBarWidth)
                    bar.Width = borderBarWidth;
                RightBorderRect = new Rectangle(RightBorderBar.Width - borderBarWidth, 0, borderBarWidth, RightBorderBar.Height);
            }
            else if (bar.Name == "BottomBorderBar")
            {
                if (bar.Height < borderBarWidth)
                    bar.Height = borderBarWidth;
                BottomBorderRect = new Rectangle(0, BottomBorderBar.Height - borderBarWidth, BottomBorderBar.Width, borderBarWidth);
            }
        }

        private void BorderBar_MouseMove(object sender, MouseEventArgs e)
        {
            Panel bar = sender as Panel;
            Point pt = new Point(e.X, e.Y);
            if (!_isDragging) return;
            if (bar.Name == "LeftBorderBar")
            {
                if (LeftBorderRect.Contains(pt))
                {
                    LeftBorderBar.Cursor = Cursors.SizeWE;
                    leftMouseMove = MouseMoveStatus.Enter;
                }
                else
                {
                    LeftBorderBar.Cursor = Cursors.Default;
                    leftMouseMove = MouseMoveStatus.Leave;
                }
                if (leftMouseDown)
                {
                    Point mousePoint = MousePosition;//鼠标相对于屏幕的坐标
                                                     //Point p2 = this.PointToClient(MousePosition);//鼠标相对于窗体的坐标
                    int width = this.Right - mousePoint.X;

                    if (NormalSize.Width > width)
                        width = NormalSize.Width;
                    else
                    {
                        this.Location = new Point(mousePoint.X, this.Location.Y);
                        this.Width = width;
                    }
                }
            }
            else if (bar.Name == "TopBorderBar")
            {
                if (TopBorderRect.Contains(pt))
                {
                    TopBorderBar.Cursor = Cursors.SizeNS;
                    topMouseMove = MouseMoveStatus.Enter;
                }
                else
                {
                    TopBorderBar.Cursor = Cursors.Default;
                    topMouseMove = MouseMoveStatus.Leave;
                }

                if (topMouseDown)
                {
                    Point mousePoint = MousePosition;//鼠标相对于屏幕的坐标
                                                     //Point p2 = this.PointToClient(MousePosition);//鼠标相对于窗体的坐标
                    int height = this.Bottom - mousePoint.Y;
                    if (NormalSize.Height > height)
                        height = NormalSize.Height;
                    else
                    {
                        this.Location = new Point(this.Location.X, mousePoint.Y);
                        this.Height = height;
                    }
                }
            }
            else if (bar.Name == "RightBorderBar")
            {
                if (RightBorderRect.Contains(pt))
                {
                    RightBorderBar.Cursor = Cursors.SizeWE;
                    leftMouseMove = MouseMoveStatus.Enter;
                }
                else
                {
                    RightBorderBar.Cursor = Cursors.Default;
                    rightMouseMove = MouseMoveStatus.Leave;
                }

                if (rightMouseDown)
                {
                    Point mousePoint = MousePosition;//鼠标相对于屏幕的坐标
                                                     //Point p2 = this.PointToClient(MousePosition);//鼠标相对于窗体的坐标
                    int width = mousePoint.X - this.Left;
                    if (NormalSize.Width > width)
                        width = NormalSize.Width;
                    else
                        this.Width = width;
                }
            }
            else if (bar.Name == "BottomBorderBar")
            {
                if (BottomBorderRect.Contains(pt))
                {
                    BottomBorderBar.Cursor = Cursors.SizeNS;
                    bottomMouseMove = MouseMoveStatus.Enter;
                }
                else
                {
                    BottomBorderBar.Cursor = Cursors.Default;
                    bottomMouseMove = MouseMoveStatus.Leave;
                }

                if (bottomMouseDown)
                {
                    Point mousePoint = MousePosition;//鼠标相对于屏幕的坐标
                                                     //Point p2 = this.PointToClient(MousePosition);//鼠标相对于窗体的坐标
                    int height = mousePoint.Y - this.Top;
                    if (NormalSize.Height > height)
                        height = NormalSize.Height;
                    else
                        this.Height = height;
                }
            }
        }

        public void SetSkin(Color color)
        {
            this.BorderColor = color;
            //this.BackColor = color;
        }

        private enum MouseMoveStatus
        {
            None,
            Enter,
            Leave,
        }
    }
}
